package com.cool.reggie.modules.shoppingCart.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cool.reggie.modules.shoppingCart.entity.ShoppingCartEntity;
import com.cool.reggie.modules.shoppingCart.service.ShoppingCartService;
import com.cool.reggie.utils.BaseContext;
import com.cool.reggie.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 1950735817@qq.com
 * @since 2022-07-18
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public ResultData shoppingList() {
        LambdaQueryWrapper<ShoppingCartEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartEntity::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCartEntity::getCreateTime);
        List<ShoppingCartEntity> list = shoppingCartService.list(queryWrapper);
        return ResultData.ok(list);
    }

    @PostMapping("/add")
    public ResultData addShopping(@RequestBody ShoppingCartEntity shoppingForm) {

        // 设置用户ID，确保是哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingForm.setUserId(currentId);


        // 查询购物车中是否已存在菜品
        Long dishId = shoppingForm.getDishId();
        LambdaQueryWrapper<ShoppingCartEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartEntity::getUserId, currentId);


        // 菜品或者套餐
        if (dishId == null) {
            queryWrapper.eq(ShoppingCartEntity::getSetmealId, shoppingForm.getSetmealId());
        } else {
            queryWrapper.eq(ShoppingCartEntity::getDishId, dishId);
        }

        ShoppingCartEntity shoppingCartEntity = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartEntity == null) {
            shoppingForm.setNumber(1);
            shoppingCartService.save(shoppingForm);
            shoppingCartEntity = shoppingForm;
        } else {
            shoppingCartEntity.setNumber(shoppingCartEntity.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartEntity);
        }

        // 如果存在，那么就在购物车中数量加一
        return ResultData.ok(shoppingCartEntity);
    }

    @PostMapping("/sub")
    public ResultData subShopping(@RequestBody ShoppingCartEntity shoppingForm) {
        Long dishId = shoppingForm.getDishId();
        Long setmealId = shoppingForm.getSetmealId();

        LambdaQueryWrapper<ShoppingCartEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (dishId != null) {
            queryWrapper.eq(ShoppingCartEntity::getDishId, dishId);
        }

        if (setmealId != null) {
            queryWrapper.eq(ShoppingCartEntity::getSetmealId, setmealId);
        }

        ShoppingCartEntity shoppingCartEntity = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartEntity != null) {
            Integer number = shoppingCartEntity.getNumber();
            if(number>1){
                shoppingForm.setNumber(number - 1);
                shoppingForm.setId(shoppingCartEntity.getId());
                shoppingCartService.updateById(shoppingForm);

            }else{
                shoppingCartService.remove(queryWrapper);
            }
        }

        return ResultData.ok(shoppingForm);
    }


    @DeleteMapping("/clean")
    public ResultData cleanShopping() {
        LambdaQueryWrapper<ShoppingCartEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartEntity::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return ResultData.ok();
    }


}
